﻿using System.Collections;
using System.Collections.Generic;

namespace SoulDoorEngine.Framework.Components
{
    public class ComponentEnumerator : IEnumerator
    {
        private int _Position = -1;
        private ComponentCollection _Collection;
        private List<int> _OrderIndex;

        /// <summary>
        /// 顺序索引
        /// </summary>
        private List<int> OrderIndex
        {
            get { return _OrderIndex; }
            set { _OrderIndex = value; }
        }
        
        /// <summary>
        /// 枚举的组件集合
        /// </summary>
        private ComponentCollection Collection
        {
            get { return _Collection; }
            set { _Collection = value; }
        }

        /// <summary>
        /// 当前位置
        /// </summary>
        private int Position
        {
            get { return _Position; }
            set { _Position = value; }
        }

        /// <summary>
        /// 枚举当前Item
        /// </summary>
        public object Current
        {
            get { return this.Collection[OrderIndex[this.Position]]; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="collection">枚举的组件集合</param>
        /// <param name="drawOrderIndex">绘制顺序索引</param>
        public ComponentEnumerator(ComponentCollection collection, List<int> drawOrderIndex)
        {
            this.Collection = collection;
            this.OrderIndex = drawOrderIndex;
        }

        /// <summary>
        /// 枚举至下个Item
        /// </summary>
        /// <returns></returns>
        public bool MoveNext()
        {
            this.Position++;
            if (this.Position >= this.OrderIndex.Count)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 重置枚举
        /// </summary>
        public void Reset()
        {
            this.Position = -1;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IEnumerator GetEnumerator()
        {
            return this;
        }
    }
}
